<?php

namespace Manage\Controller;

use Manage\Controller\BaseController;

class ManageController extends BaseController {
    
    public function _initialize() {
//        parent::_initialize();
//        if (!$this->mid) {
//            $this->error('请先登录');
//        }
    }
    
    //商户统计
    public function personNum(){
        $data['total_num'] = M('seller')->count();
        $todayWhere['create_time'] = array('EGT',date('Y-m-d 00:00:00'));
        $data['today_num'] = M('seller')->where($todayWhere)->count();
        $this->returnList($data);
    }
    
    //删除用户
    public function deleteUser(){
        $user_id = $this->getParam('user_id', 0, 'int+', TRUE, '请选择用户ID');
        $res = M('user')->where(['user_id'=>$user_id])->delete();
        if($res){
            $this->success();
        }else{
            $this->error();
        }
    }
    
    //用户列表
    //type: 1今日用户 2本周用户  3本月用户 4所有
    public function userList(){
        $postData = $_POST;
        $page = intval($postData['page']) ? intval($postData['page']) : 1;
        $size = intval($postData['size']) ? intval($postData['size']) : 10;
        $type = $this->getParam('type', 0, 'int+');
        
        $nowDate = date("Y-m-d 00:00:00");
        $monWeekDate = date('Y-m-d 00:00:00', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));;
        $monMonDate = date('Y-m-01 H:i:s', strtotime(date("Y-m-d")));
        $where =  [];
        switch ($type) {
            case '1':
                $where['create_time'] = array('EGT',$nowDate);
                break;
            case '2':
                $where['create_time'] = array('EGT',$monWeekDate);
                break;
            case '3':
                $where['create_time'] = array('EGT',$monMonDate);
                break;
            default:
                break;
        }
        !empty($postData['city']) && $where['city'] = array("IN",[$postData['city'], '']);
        
        $list =M('user')->where($where)->order('user_id DESC')->limit($size)->page($page)->select();
        $this->returnList($list);
    }
    
    /**
     * 用户统计列表
     * type: 1今日用户 2本周用户  3本月用户 4所有
     */
    public function userStatisList(){
        $nowDate = date("Y-m-d 00:00:00");
        $monWeekDate = date('Y-m-d 00:00:00', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
        $monMonDate = date('Y-m-01 H:i:s', strtotime(date("Y-m-d")));
        
        
        $return['total_num'] = M('user')->count();
        $return['today_num'] = M('user')->where(['create_time'=>array('EGT',$nowDate)])->count();
         $return['week_num'] = M('user')->where(['create_time'=>array('EGT',$monWeekDate)])->count();
        $return['month_num'] = M('user')->where(['create_time'=>array('EGT',$monMonDate)])->count();
        $this->returnList($return);
    }
    
    //拉黑商户
    public function unableTenant(){
        $wid = $this->getParam('wid', 0, 'int+', TRUE, '请选择商户ID');
        $state =  $this->getParam('state', 0, 'int+', TRUE, '请选择状态');
        $this->_authSign($wid.$state);
        $res = M('seller')->where(['wid'=>$wid])->save(['state'=>$state]);
        if($res){
            $this->success();
        }else{
            $this->error();
        }
    }
    
    //拉黑用户
    public function unableUser(){
        $user_id = $this->getParam('user_id', 0, 'int+', TRUE, '请选择用户ID');
        $state =  $this->getParam('state', 0, 'int+', TRUE, '请选择状态');
        $res = M('user')->where(['user_id'=>$user_id])->save(['state'=>$state]);
        if($res){
            $this->success();
        }else{
            $this->error();
        }
    }
    
    
    //订单详情
    public function orderDetail(){
        $order_id = $this->getParam('order_id', 0, 'int+', TRUE, '请选择订单ID');
        $where['order_id'] = $order_id;
        $orderInfo = M('order')
                    ->alias('o')
                    ->field('o.*, s.nickname,s.headpic,s.business_phone,s.address,s.lat,s.lon,s.isonwork,u.headpic as user_pic,u.nickname as user_name,u.user_phone,')
                    ->join('ot_seller AS s ON s.wid = o.wid')
                    ->join('ot_user AS u ON u.user_id = o.user_id')
                    ->where($where)
                    ->find();
        $this->returnList($orderInfo);
    }

    //订单列表
    public function orderList(){
        $postData = $_POST;
        $page = intval($postData['page']) ? intval($postData['page']) : 1;
        $size = intval($postData['size']) ? intval($postData['size']) : 10;
        $state = $this->getParam('state', 0);
        $where['state'] = $state;
        $list = M('order')
                ->alias('o')
                ->field('o.*, s.nickname,s.headpic,s.business_phone,s.address,s.lat,s.lon,s.isonwork')
                ->join('ot_seller AS s ON s.wid = o.wid')
                ->where($where)
                ->limit($size)
                ->page($page)
                ->select();
        $this->returnList($list);
    }
    
    //举报列表
    public function reportList(){
        $postData = $_POST;
        $page = intval($postData['page']) ? intval($postData['page']) : 1;
        $size = intval($postData['size']) ? intval($postData['size']) : 10;
        $where = [];
        !empty($postData['city']) && $where['city'] = array("IN",[$postData['city'], '']);
        if(intval($postData['wid'])){
            $where['r.wid'] = intval($postData['wid']);
        }
//        $list = M('report')->alias('r')->field('r.*,u.user_phone as report_phone,u.nickname as report_name')
//                ->join('ot_user AS u ON u.user_id = r.user_id')
//                ->where($where)
//                ->limit($size)
//                ->page($page)
//                ->select();
        $list = M('report')->alias('r')->field('r.*,s.business_phone,s.nickname')
                ->join('ot_seller AS s ON s.wid=r.wid')
                ->where($where)
                ->limit($size)
                ->page($page)
                ->order("id DESC")
                ->select();
        
        $this->returnList($list);
        
    }
 

    //删除商户
    public function deleteTenant(){
        $wid = $this->getParam('wid', 0, 'int+', TRUE, '请选择商户ID');
        $res = M('seller')->where(['wid'=>$wid])->delete();
        if($res){
            $this->success();
        }else{
            $this->error();
        }
    }
    
    //商户详情
    public function personDetail(){
        $wid = $this->getParam('wid', 0, 'int+', TRUE, '请选择商户ID');
        $admin_id = $this->getParam('admin_id',0,'int+',TRUE,'管理员ID必须');
        
        $lat = I('lat');
        $lon = I('lon');
        $field = "wid,business_phone,nickname,headpic,age,height,weight,bust,price,xingzuo,old_address,address,detail_address,ishome,arr_img,update_time,create_time,isonwork,state,video_url,video_img,intro,lon,lat,top,auth_state,remark,qq,qqnum,receivenum,wx,city,activityid,gender,recommend_uid";
        $field .= ",movie,music,book,motion,game,place,tags,appearance,zodiac,idol,hobby,job,income,house,blood";
        if($lat && $lon){
            $field .= ",(round(6367000 * 2 * asin(sqrt(pow(sin(((lat * pi()) / 180 - ({$lat} * pi()) / 180) / 2), 2) + cos(({$lat} * pi()) / 180) * cos((lat * pi()) / 180) * pow(sin(((lon * pi()) / 180 - ({$lon} * pi()) / 180) / 2), 2))))) AS distance";
        }
        $sellerInfo = M('seller')->field($field)->where(array('wid'=>$wid))->find();
        if($sellerInfo){
            $sellerInfo['arr_img'] = explode(',', $sellerInfo['arr_img']);
            $sellerInfo['score'] = getAvgScore($wid);
            $collectionInfo = M("admin_collection")->where(['admin_id'=>$admin_id,'wid'=>$wid])->find();
            if(!empty($collectionInfo)){
                $sellerInfo['iscollect'] = 1;
            }else{
                $sellerInfo['iscollect'] = 0;
            }
        }
        $this->returnList($sellerInfo);
    }
    
    /**
     * 商户列表
     * type: 1 今日用户 2所有用户 3收藏的用户 4被举报的用户 5搜索用户
     * search_type:搜索类型 1 按照昵称，2按照手机号
     */
    public function tenantList(){
        $postData = $_POST;
        $page = intval($postData['page']) ? intval($postData['page']) : 1;
        $size = intval($postData['size']) ? intval($postData['size']) : 10;
        $search_type = intval($postData['search_type']) ? intval($postData['search_type']) : 0;
        $type = $this->getParam('type', 0, 'int+');
        $nowDate = date("Y-m-d 00:00:00");
        
        if($search_type == 1){  //搜索类型 1 按照昵称，2按照手机号 
            isset($postData['key']) && $where['nickname'] = array('like',"%{$postData['key']}%");
        }elseif($search_type == 2){
            isset($postData['key']) && $where['business_phone'] = array('like',"%{$postData['key']}%");
        }
        
        isset($postData['gender']) && $where['gender'] = $postData['gender'];
        !empty($postData['city']) && $where['city'] = array("IN",[$postData['city'], '']);
        
        switch ($type) {
            case '1':   //今日
                $where['create_time'] = array('EGT',$nowDate);
                break;
            case '3': //收藏的用户
                $arrWid = M('admin_collection')->field('wid')->select();
                if(!empty($arrWid)){
                    foreach($arrWid as $val){
                        $wids[] = $val['wid'];
                    }
                    $where['wid'] = array('IN',$wids);
                }
                break;
            case '4':
                $arrWid = M('report')->field('wid')->select();
                if(!empty($arrWid)){
                    foreach($arrWid as $val){
                        $wids[] = $val['wid'];
                    }
                    $where['wid'] = array('IN',$wids);
                }
                break;
            default:
                break;
        }
        $sort = "top DESC,wid DESC";
        $list = M('seller')->field("password,pay_password",TRUE)->where($where)->order($sort)->limit($size)->page($page)->select();
        if($list){
            foreach($list as &$val){
                $val['arr_img'] = explode(',', $val['arr_img']);
                $val['scroe'] = getAvgScore($val['wid']);
            }
            unset($val);
        }
        $this->returnList($list);
    }
    
    //添加收藏
    public function addCollection(){
        $wid = $this->getParam('wid',0,'int+',TRUE,'选择商品ID');
        $admin_id = $this->getParam('admin_id',0,'int+',TRUE,'管理员ID必须');
        $where['admin_id'] = $admin_id;
        $where['wid'] = $wid;
        $collectionInfo = M('admin_collection')->where($where)->find();
        if($collectionInfo){
            $this->error('已收藏');
        }
        $data = $where;
        $res = M('admin_collection')->add($data);
        if($res){
            $this->success();
        }else{
            $this->error();
        }
    }
    
    //取消收藏
    public function cancelCollection(){
        $wid = $this->getParam('wid',0,'int+',TRUE,'选择商品ID');
        $admin_id = $this->getParam('admin_id',0,'int+',TRUE,'管理员ID必须');
        $where['admin_id'] = $admin_id;
        $where['wid'] = $wid;
        $res = M('admin_collection')->where($where)->delete();
        $this->success();
    }
    
    //意见列表
    public function suggestList(){
        $list = M('feedback')->order("feedback_id desc")->select();
        
        $this->returnList($list);
    }
    
    //设置是否公开评价
    public function setCanPublicEvalution(){
        $evalution_state = $this->getParam('evalution_state',0,'int+',TRUE,'evalution_state参数必须');
        $res = M('config')->where(['config_key'=>'evalution_state'])->save(['config_value'=>$evalution_state]);
        $this->success();
    }
    
    //获取是否公开评价
    public function getCanPublicEvalution(){
        $value = M('config')->where(['config_key'=>'evalution_state'])->getField('config_value');
        $this->returnList($value);
    }
    
    //浏览数
    public function addvisit(){
        $wid = $this->getParam('wid',0,'int+',TRUE,'wid参数必须');
        $where['wid'] = $wid;
        $where['hit_time'] = date("Y-m-d");
        $sellerHitInfo = M("sellerHit")->where($where)->find();
        if(!empty($sellerHitInfo)){
            M("sellerHit")->where($where)->setInc("num",1);
        }else{
            $data = $where;
            $data['num'] = 1;
            M("sellerHit")->add($data);
        }
        
        M("seller")->where(['wid'=>$wid])->setInc("num",1);
        $this->success();
    }
    
    //浏览数
    public function visitnum(){
        $postData = $_POST;
        $page = intval($postData['page']) ? intval($postData['page']) : 1;
        $size = intval($postData['size']) ? intval($postData['size']) : 10;
        $list = M("seller")->field("wid,num")->page($page)->limit($size)->select();
        if(empty($list)){
            $this->returnList($list);
        }
        $todayTime = date("Y-m-d");
        foreach($list as &$val){
            $val['num'] = intval($val['num']);
            $todaynum = M("sellerHit")->where(['wid'=>$val['wid'],'hit_time'=>$todayTime])->getField('num');
            $val['todaynum'] = intval($todaynum);
        }
        unset($val);
        $this->returnList($list);
    }
    
    
    public function delEval(){
        $eval_id = $this->getParam('eval_id','0','int+',TRUE,'选中要删除的评论ID');
        M('order_eval')->where(['eval_id'=>$eval_id])->delete();
        $this->success();
    }
    
    //置顶
    public function setTop(){
        $wid = $this->getParam('wid',0,'int+',TRUE,'选择商品ID');
        $top = $this->getParam('top',0,'int+',TRUE,'置顶数必须');
        M('seller')->where(['wid'=>$wid])->save(['top'=>$top]);
        $this->success();
    }
    
    //取消置顶
    public function cancelTop(){
        $wid = $this->getParam('wid',0,'int+',TRUE,'选择商品ID');
        M('seller')->where(['wid'=>$wid])->save(['top'=>0]);
        $this->success();
    }
    
    public function setAuth(){
        $wid = $this->getParam('wid',0,'int+',TRUE,'选择商品ID');
        $auth_state = I('auth_state',0,'intval');
        
        M('seller')->where(['wid'=>$wid])->save(['auth_state'=>$auth_state]);
        $this->success();
    }
    
    //黑名单
    public function blacklist(){
        $postData = $_POST;
        $page = intval($postData['page']) ? intval($postData['page']) : 1;
        $size = intval($postData['size']) ? intval($postData['size']) : 10;
        !empty($postData['city']) && $where['city'] = array("IN",[$postData['city'], '']);
        $where['state'] = 2;
        $list = M('seller')->field("password,pay_password",TRUE)->where($where)->order("wid DESC")->limit($size)->page($page)->select();
        $this->returnList($list);
    }
    
    public function setContentTop(){
        $id = $this->getParam('id',0,'int+',TRUE,'id必须');
        $this->_authSign($id);
        M('seller_content')->where(['id'=>$id])->save(['top'=>1]);
        $this->success();
    }
    
}
